package com.zslc.securitymonitoring.scheduling;

import cn.hutool.core.collection.CollectionUtil;
import com.zslc.securitymonitoring.dao.*;
import com.zslc.securitymonitoring.vo.GlobalSeverityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时合并告警信息表新数据
 */
@EnableScheduling
@Component
@Slf4j
public class syncTableData {
    @Autowired
    private AvailabilityMapper availabilityMapper;
    @Autowired
    private PhishingMapper phishingMapper;
    @Autowired
    private SpamlinkMapper spamlinkMapper;
    @Autowired
    private TrojanMapper trojanMapper;
    @Autowired
    private VulMapper vulMapper;

    /**
     * 有效性新数据每5min合并一次,因为有效性5min扫描一次
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void syncAvailabilityTask() {
        log.info("【合并alert_availabilities新数据到alert_availabilities_merge表】任务开始。。。");
        long starttime = System.currentTimeMillis();
        try {
            Integer num = 0;
            List<GlobalSeverityVO> list = availabilityMapper.selectNewData();
            List<Integer> idList = list.parallelStream().map(GlobalSeverityVO::getId).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(idList)) {
                availabilityMapper.insertNewData(idList);
            }
            log.info("【合并alert_availabilities新数据到alert_availabilities_merge表】任务结束,合并后新数据【{}】条，耗时【{}ms】",num,System.currentTimeMillis()-starttime);
        } catch (Exception e) {
            log.error("【合并alert_availabilities新数据到alert_availabilities_merge表】任务出错。",e);
        }
    }

    /**
     * 黑链，敏感词两小时扫描一次，新数据统一10分钟查找合并一次
     */
    @Scheduled(cron = "0 */10 * * * ?")
    public void syncOtherTask() {
        log.info("【合并alert_phishings新数据到alert_phishings_merge表】任务开始。。。");
        long starttime1 = System.currentTimeMillis();
        try {
            Integer num1 = 0;
            List<GlobalSeverityVO> list1 = phishingMapper.selectNewData();
            List<Integer> idList1 = list1.parallelStream().map(GlobalSeverityVO::getId).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(idList1)) {
                num1 = phishingMapper.insertNewData(idList1);
            }
            log.info("【合并alert_phishings新数据到alert_phishings_merge表】任务结束,合并后新数据【{}】条，耗时【{}ms】",num1,System.currentTimeMillis()-starttime1);
        } catch (Exception e) {
            log.error("【合并alert_phishings新数据到alert_phishings_merge表】任务出错。",e);
        }

        log.info("【合并alert_spamlinks新数据到alert_spamlinks_merge表】任务开始。。。");
        long starttime2 = System.currentTimeMillis();
        try {
            Integer num2 = 0;
            List<GlobalSeverityVO> list2 = spamlinkMapper.selectNewData();
            List<Integer> idList2 = list2.parallelStream().map(GlobalSeverityVO::getId).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(idList2)) {
                num2 = spamlinkMapper.insertNewData(idList2);
            }
            log.info("【合并alert_spamlinks新数据到alert_spamlinks_merge表】任务结束,合并后新数据【{}】条，耗时【{}ms】",num2,System.currentTimeMillis()-starttime2);
        } catch (Exception e) {
            log.error("【合并alert_spamlinks新数据到alert_spamlinks_merge表】任务出错。",e);
        }

        log.info("【合并alert_trojans新数据到alert_trojans_merge表】任务开始。。。");
        long starttime3 = System.currentTimeMillis();
        try {
            Integer num3 = 0;
            List<GlobalSeverityVO> list3 = trojanMapper.selectNewData();
            List<Integer> idList3 = list3.parallelStream().map(GlobalSeverityVO::getId).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(idList3)) {
                num3 = trojanMapper.insertNewData(idList3);
            }
            log.info("【合并alert_trojans新数据到alert_trojans_merge表】任务结束,合并后新数据【{}】条，耗时【{}ms】",num3,System.currentTimeMillis()-starttime3);
        } catch (Exception e) {
            log.error("【合并alert_trojans新数据到alert_trojans_merge表】任务出错。",e);
        }
    }

    /**
     * 漏洞新数据每天(23点)合并一次，因为漏洞扫描7天一次
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void syncVulTask() {
        log.info("【合并alert_vuls新数据到alert_vuls_merge表】任务开始。。。");
        long starttime = System.currentTimeMillis();

        try {
            Integer num = 0;
            List<GlobalSeverityVO> list = vulMapper.selectNewData();
            List<Integer> idList = list.parallelStream().map(GlobalSeverityVO::getId).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(idList)) {
                num = vulMapper.insertNewData(idList);
            }
            log.info("【合并alert_vuls新数据到alert_vuls_merge表】任务结束,合并后新数据【{}】条，耗时【{}ms】", num, System.currentTimeMillis() - starttime);
        } catch (Exception e) {
            log.error("【合并alert_vuls新数据到alert_vuls_merge表】任务出错。",e);
        }
    }

}
